package comp
{
	use namespace comp_layout;
	
	import comp.Interface.IDispose;
	import comp.style.css.CssAchieve;
	import comp.style.css.MergeLayout;
	
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TextEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	/**
	 * <h1>输入控件</h1>
	 * 支持背景，文本样式和Padding;
	 * 
	 * 示例:webgame.shop.ShopInput 133行
	 * @author:pu</br>
	 * date:2010.8.24 17:47
	 */
	public class InputText extends UIComponent
	{
		include "style/TextStyles"
		include "style/BorderStyles";
		include "style/PaddingStyles";
		include "style/SkinStyle" //皮肤属性

		//***********样式****************

		//public
		public function InputText()
		{
			super();
			this._themeName="comp::InputText";
		}

		public function get maxChars():int
		{
			return _maxChars;
		}

		public function set maxChars(value:int):void
		{
			_maxChars = value;
		}

		public override function set height(value:Number):void
		{
			super.height = value;
			if(this.initialized)
				merge();
		}

		public  override function set width(value:Number):void
		{
			super.width = value;
			if(this.initialized)
				merge();
		}
		
		public function get text():String
		{
			return 	_text;
		}
		
		public function set text(value:String):void
		{
			_text=value;
			
			if(initialized)
			{
				commitProperties();
				merge();
			}
		}

		public override function dispose():void
		{
			super.dispose();
			_textField.removeEventListener(Event.CHANGE,onChange);
			this._textField=null;
		}
		
		/**
		 * 设置输入文本改变事件
		 */
		public function get onTextChange():Function
		{
			return _onTextChange;
		}
		
		public function set onTextChange(value:Function):void
		{
			_onTextChange = value;
		}
		
		override public function set name(value:String):void
		{
			super.name=value;
			textField.name=value;
		}
		
		//protected
		override protected function createChildren():void
		{
			textField.type=TextFieldType.INPUT;
			textField.addEventListener(Event.CHANGE,onChange);
			addChild(textField);
		}

		override protected function commitProperties():void
		{
			if(_text!=null && _textField)
				_textField.text=_text;
		}
		
		override protected function commitStyles():void
		{
			CssAchieve.mergeTextField(_textField,this);		
			_textField.maxChars=getStyle("maxChars");		
			
		}
		
		override protected function merge():void
		{
			MergeLayout.labelLayout(_textField,this);
			CssAchieve.mergeTemplate(this);
			CssAchieve.mergeBackGrounde(this);
			this.wholeRect=new Rectangle(0,0,this.width,this.height);
		}

		protected function onChange(e:Event):void
		{
			_text=_textField.text;
			if(_onTextChange!=null)
				_onTextChange(e);
		}
		
		public function get textField():TextField
		{
			if(_textField==null)
				_textField=new TextField();
			return _textField;
		}
		
		//private
		private var _textField:TextField;
		private var _text:String="";
		private var _onTextChange:Function;
		private var _maxChars:int=0;
		
		///////////////////////////////////
		
	}
}